package com.xm.xmapp.tools;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import com.xm.xmapp.app.Constants;
import com.xm.xmapp.request.bean.BaseBeanRequest;

public class BeanTools {

	public static Map<String, String> getMap(BaseBeanRequest o) {
		Map<String, String> map = new HashMap<String, String>();
		try {

			Field[] fields = o.getClass().getFields();

			for (int i = 0; i < fields.length; i++) {

				String firstLetter = fields[i].getName();
				Object value = fields[i].get(o);
				if (value != null)
					map.put(firstLetter, value.toString());
			}
			StringBuilder encodedParams = new StringBuilder();
			StringBuilder str = new StringBuilder();
			for (Map.Entry<String, String> entry : map.entrySet()) {
				if (!URLEncoder.encode(entry.getKey(), "UTF-8").equals("sign")) {
					encodedParams.append(URLEncoder.encode(entry.getKey(),
							"UTF-8"));
					encodedParams.append('=');
					encodedParams.append(entry.getValue());
					encodedParams.append('&');
				}
			}
			str = encodedParams.append(Constants.certkey);
			String sign = md5(str.toString());
			map.put("sign", sign);
			o.sign = sign;
		} catch (Exception e) {
			//
		}
		return map;

	}

	public static StringBuilder getStr(Object o) throws IllegalAccessException,
			IllegalArgumentException {

		StringBuilder encodedParams = new StringBuilder();

		Field[] fields = o.getClass().getFields();

		for (int i = 0; i < fields.length; i++) {

			String firstLetter = fields[i].getName();
			Object value = fields[i].get(o);
			if (value != null) {
				if (!firstLetter.equals("sign")) {
					encodedParams.append(firstLetter + "=" + value.toString()
							+ "&");
				}
			}
		}

		String str = encodedParams + Constants.certkey;
		String sign = md5(str);
		encodedParams.append("sign=" + sign);

		return encodedParams;

	}

	public static String getStrTest(Object o) {
		// Map<String, String> map = new HashMap<String, String>();
		String str = "";
		try {

			Field[] fields = o.getClass().getDeclaredFields();

			for (int i = 0; i < fields.length; i++) {

				String firstLetter = fields[i].getName();
				Object value = fields[i].get(o);
				if (value != null) {
					str = str + firstLetter + "=" + value + "&";
				}

			}

		} catch (Exception e) {

		}
		return str;

	}

	// 加密方法
	public static String md5(String string) {
		byte[] hash;
		try {
			hash = MessageDigest.getInstance("MD5").digest(
					string.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("Huh, MD5 should be supported?", e);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("Huh, UTF-8 should be supported?", e);
		}

		StringBuilder hex = new StringBuilder(hash.length * 2);
		for (byte b : hash) {
			if ((b & 0xFF) < 0x10)
				hex.append("0");
			hex.append(Integer.toHexString(b & 0xFF));
		}
		return hex.toString();
	}
}
